import { defineStore } from "pinia";
import { ElMessage } from "element-plus";
import { setSession, getSession } from "@/utils/storage";
import getRoutePool from "@/assets/js/handleData.js";
import { toRaw } from "vue";

export const useAppStore = defineStore("App", {
  state: () => {
    return {
      sidebarOpened: getSession("__sidebarOpened__", true),
      tagsViewList: getSession("__tagsViewList__", []),
    };
  },
  actions: {
    changeSidebarOpened() {
      setSession("__sidebarOpened__", this.sidebarOpened);
      this.sidebarOpened = !this.sidebarOpened;
    },
    addTagViewList(tag) {
      // tag 将要新增的 tag
      const routePool = getRoutePool();

      const isFind = routePool.find((item) => item.path === tag.path);
      if (!isFind) return;
      // 判断是否在 tagsViewList 已经存在
      const isExit = this.tagsViewList.find((item) => item.path === tag.path);

      if (!isExit) {
        // 不存在时
        this.tagsViewList.push(tag);
        setSession("__tagsViewList__", toRaw(this.tagsViewList));
      }
    },
    removeTagViewList(type, index) {
      let tagsViewList = this.tagsViewList;
      // 删自己
      switch (type) {
        case "me":
          tagsViewList.splice(index, 1);
          break;
        case "other":
          tagsViewList = tagsViewList.splice(index, 1);
          break;
        case "right":
          tagsViewList.splice(index + 1);
          break;
      }
      this.tagsViewList = tagsViewList;
      setSession("__tagsViewList__", toRaw(this.tagsViewList));
    },
  },
  // persist: {
  //   enabled: true,
  //   strategies: [{ Storage: localStorage, path: ["sidebarOpened"] }],
  // },
});
